#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#define len 10

extern float duration;
extern int RANKmode;
//定义结构体
typedef struct 
{   
    float time;
    char name[50];
}players;

players player[len];

players tmp;

extern char name1[50];


//判断时间是否能冲进前十
bool is_topten(){
    //定义一个有效数据的个数
    int valid_data;
    FILE *fp1,*fp2,*fp3;
    //定义三个文件指针
    fp1 = fopen("gamedata\\Rank_easydata.txt","r"); 
    fp2 = fopen("gamedata\\Rank_middata.txt","r"); 
    fp3 = fopen("gamedata\\Rank_harddata.txt","r");
    //选择模式
    for(valid_data = 0;valid_data<len;valid_data++){
        if(RANKmode == 1){
            if((fscanf(fp1,"%s%f",tmp.name,&tmp.time))!=EOF)
                player[valid_data] = tmp;
            else break;
        }
        if(RANKmode == 2){
            if((fscanf(fp2,"%s%f",tmp.name,&tmp.time))!=EOF)
                player[valid_data] = tmp;
            else break;
        }
        if(RANKmode == 3){
            if((fscanf(fp3,"%s%f",tmp.name,&tmp.time))!=EOF)
                player[valid_data] = tmp;
            else break;
        }
    }
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);
    
    if(valid_data < 10)
        return true;
    //比较时间
    else{
        if(duration < player[valid_data-1].time)
        return true;
        else{ 
        
        return false;
        }
    }

}




void sort(){
    int valid_data;
    printf("time:%f\n",duration);
    //不同模式读取不同文件
    
        
    FILE *fp1,*fp2,*fp3,*fp4;
    fp1 = fopen("gamedata\\Rank_easydata.txt","r");
    fp2 = fopen("gamedata\\Rank_middata.txt","r");
    fp3 = fopen("gamedata\\Rank_harddata.txt","r");
    for(valid_data = 0;valid_data<len;valid_data++){
        if(RANKmode == 1){        
            if((fscanf(fp1,"%s%f",tmp.name,&tmp.time))!=EOF)
                player[valid_data] = tmp;
            else break;
        }
        if(RANKmode == 2){
            if((fscanf(fp2,"%s%f",tmp.name,&tmp.time))!=EOF)
                player[valid_data] = tmp;
            else break;
        }
        if(RANKmode == 3){
            if((fscanf(fp3,"%s%f",tmp.name,&tmp.time))!=EOF)
                player[valid_data] = tmp;
            else break;
        }
    }
    if(RANKmode == 1)
        fp4 = fopen("gamedata\\Rank_easydata.txt","w");
    else if(RANKmode == 2)
        fp4 = fopen("gamedata\\Rank_middata.txt","w");        
    else if(RANKmode == 3)
        fp4 = fopen("gamedata\\Rank_harddata.txt","w");
//无有效数据
    if(valid_data == 0){
        strcpy(player[0].name,name1);
        player[0].time = duration;
        fprintf(fp4,"%s %.2f",player[0].name,player[0].time);
        
    return;
    }
//找到输入的time在文件里排的位置
    int j;
    for(j = 0;j < valid_data;j++){
        if(player[j].time > duration)
        break;
        else continue;
    }
//只在文件中存len（10）个有效数据    
//数据已满
    if(valid_data == len){
        if(j == len-1){
            player[len].time = duration;
            strcpy(player[len].name,name1);
        }
        else{
            for(int h = valid_data-1;h > j;h --){
                player[h] = player[h-1];
            }
            player[j].time = duration;
            strcpy(player[j].name,name1);
        }
        for(int count = 0;count < valid_data;count++){
        fprintf(fp4,"%s %.2f\n",player[count].name,player[count].time);
        }
        return;
    }
    
    for(int h = valid_data;h > j;h --){
        player[h] = player[h-1];
    }
    player[j].time = duration;
    strcpy(player[j].name,name1);
    
    for(int count = 0;count <= valid_data;count++){
        fprintf(fp4,"%s %.2f\n",player[count].name,player[count].time);
    }
    
    
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);
    fclose(fp4);
    
    return;
}


